package demo1228.pm;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*
 访问static修饰的共享数据.
 */
public class Demo_04 {
    public static void main(String[] args) {

        Thread th1 = new Thread(new Sale(), "窗口A");
        Thread th2 = new Thread(new Sale(), "窗口B");
        Thread th3 = new Thread(new Sale(), "窗口C");

        th1.start();
        th2.start();
        th3.start();
    }
}

class Sale implements Runnable {

    private static int ticketNum = 1000;
    private static Lock lock = new ReentrantLock();

    @Override
    public void run() {

        while (ticketNum > 0) {
            lock.lock();
            try {
                if (ticketNum > 0) {
                    System.out.println(Thread.currentThread().getName() + "正在卖"
                            + ticketNum + "张票," +
                            "还有" + --ticketNum + "张票");
                }
            } finally {
                lock.unlock();
            }
        }


    }
}
